package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import connexionsBdd.Mysql;

import personne.Personne;

public class DaoPersonneMysql implements DaoPersonne {
	private static String SQL_GET = "select * from personne where code=?";
	private static String SQL_INSERT = "insert into personne (nom,prenom,adresse) values(?,?,?)";
	private static String SQL_LOAD =  "Select * from personne where nom=?";
	private static String SQL_DELETE = "Delete from personne where code=?";

	public Personne get(int code) {
		// TODO Auto-generated method stub
		try{


			PreparedStatement req = Mysql.getInstance().prepareStatement(SQL_GET);
			req.setInt(1,code);
			ResultSet res = req.executeQuery();
			if(res.next())
			{
				Personne p = new Personne(res.getInt("code"),res.getString("nom"), res.getString("prenom"), res.getString("adresse"));
				return p;
			}


		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public ArrayList<Personne> load(String nom) {
		// TODO Auto-generated method stub
		try {
			PreparedStatement req = Mysql.getInstance().prepareStatement(SQL_LOAD);
			req.setString(1, nom);
			ResultSet res = req.executeQuery();
			ArrayList<Personne> personnes = new ArrayList<Personne>();
			while(res.next())
			{
				Personne p = new Personne(res.getInt("code"),res.getString("nom"),res.getString("prenom"),res.getString("adresse"));
				personnes.add(p);
			}
			return personnes;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

	@Override
	public void save(Personne personne) {
		// TODO Auto-generated method stub
		try
		{
			if(personne.getCode()!=-1)
				throw new IllegalArgumentException();

			PreparedStatement req;

			req = Mysql.getInstance().prepareStatement(SQL_INSERT,new String[]{"code"});
			req.setString(1,personne.getNom());
			req.setString(2, personne.getPrenom());
			req.setString(3, personne.getAdresse());
			
			req.executeUpdate();
			ResultSet res = req.getGeneratedKeys();
			if(res.next())
			{
				personne.setCode(res.getInt(1));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}

	@Override
	public void saveOrUpdate(Personne personne) {
		// TODO Auto-generated method stub
		

	}

	@Override
	public void remove(Personne personne) {
		// TODO Auto-generated method stub
		try {
			if(personne.getCode() == -1)
				throw new IllegalArgumentException();
			PreparedStatement req = Mysql.getInstance().prepareStatement(SQL_DELETE);
			req.setInt(1, personne.getCode());
			if(req.executeUpdate() == 1)
			{
				personne.setCode(-1);
			}
			req.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		

	}

	@Override
	public ArrayList<Personne> loadAll() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
